home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 0411.ZIP / FILL.C < prev    next >
Text File  |  1985-06-01  |  3KB  |  161 lines

  1. /*
  2.  * Program to read a text file and fill each text line to n chars.
  3.  */
  4. #include "stdio.h"
  5. #define CR '\15'
  6. #define LF '\12'
  7. #define SPACE '\40'
  8. main(argc,argv)
  9.     int argc;
  10.     char *argv[];
  11.     {
  12.     int  c,
  13.      holdc,
  14.      outfile,
  15.      infile,
  16.      linelenf,
  17.      cntr;
  18.     if (argc == 1)
  19.     {
  20.     puts("\n");
  21.     puts("Syntax   : FILL <infile> <outfile> [<n>]\n\n");
  22.     puts("Where    : infile  is the filespec for the input data\n");
  23.     puts("           outfile is the filespec for the output data\n\n");
  24.     puts("           n       is the length to which lines are\n");
  25.     puts("                   to be padded\n");
  26.     puts("Defaults : n=80\n");
  27.     puts("\n");
  28.     puts("FILL reads each  character from the input file  until it\n");
  29.     puts("sees a  carriage  return  (Hex 0D).  It then  checks the\n");
  30.     puts("current line  length; if  it is less than the  specified\n");
  31.     puts("length, it pads it out with blanks.  The CR and the next\n");
  32.     puts("character after it from the  input file  are  discarded.\n");
  33.     puts("The output line is then  terminated with a CR and a line\n");
  34.     puts("feed (Hex 0A).\n\n");
  35.     exit(0);
  36.     puts("\nAuthor  : Peter Townsend");
  37.     puts("\nDate    : 01Jun85");
  38.     puts("\nVersion : 01.00");
  39.     puts("\n");
  40.     exit(0);
  41.     }
  42.     if (argc == 2)
  43.     {
  44.     puts("\n");
  45.     puts("Missing Output Filename\n");
  46.     exit(1);
  47.     }
  48.     if ((infile = open(argv[1],0)) == ERR)
  49.     {
  50.     puts("\n");
  51.     puts("Cannot open input file ");
  52.     puts(argv[1]);
  53.     exit(1);
  54.     }
  55.     if ((outfile = open(argv[2],1)) == ERR)
  56.     {
  57.     if ((outfile = creat(argv[2])) == ERR)
  58.         {
  59.         puts("\n");
  60.         puts("Cannot open/create output file ");
  61.         puts(argv[2]);
  62.         exit(1);
  63.         }
  64.     }
  65.     if (argc == 3)
  66.     {
  67.     linelenf = 80;
  68.     }
  69.     else
  70.     {
  71.     linelenf = 0;
  72.     cntr = 0;
  73.     while (((argv[3][cntr]) != '\0') && (cntr <= 2))
  74.         {
  75.         if (!isdigit(argv[3][cntr]))
  76.         {
  77.         puts("\n");
  78.         puts("Parameter must be a number\n");
  79.         exit(1);
  80.         }
  81.         linelenf = (linelenf * 10) + atoi(argv[3][cntr]);
  82.         ++cntr;
  83.         }
  84.     if (linelenf > 255)
  85.         {
  86.         puts("\n");
  87.         puts("Line length should not be greater than 255\n");
  88.         exit(1);
  89.         }
  90.     }
  91.     /*
  92.      * convert the file
  93.      */
  94.     cntr = 1;
  95.     while ((c = fgetc(infile)) != EOF)
  96.     {
  97.     /*
  98.      * check for end of line
  99.      */
  100.     if (c == CR)
  101.         {
  102.         fgetc(infile);
  103.         c = SPACE;
  104.         /*
  105.          * pad line out to desired length
  106.          */
  107.         while (cntr <= linelenf)
  108.         {
  109.         putc(c,outfile);
  110.         ++cntr;
  111.         }
  112.         c = CR;
  113.         putc(c,outfile);
  114.         c = LF;
  115.         putc(c,outfile);
  116.         cntr = 1;
  117.         }
  118.     else
  119.         {
  120.         /*
  121.          * check for a given line longer than desired length
  122.          */
  123.          if (cntr == linelenf)
  124.         {
  125.         holdc = c;
  126.         c = CR;
  127.         putc(c,outfile);
  128.         c = LF;
  129.         putc(c,outfile);
  130.         c = holdc;
  131.         putc(c,outfile);
  132.         cntr = 1;
  133.         }
  134.         else
  135.         {
  136.         putc(c,outfile);
  137.         ++cntr;
  138.         }
  139.         }
  140.     }
  141.     /*
  142.      * check that last line input was the correct length
  143.      */
  144.     c = SPACE;
  145.     while ((cntr < linelenf) && (cntr != 1))
  146.     {
  147.     putc(c,outfile);
  148.     ++cntr;
  149.     }
  150.     if (cntr != 0)
  151.     {
  152.     c = CR;
  153.     putc(c,outfile);
  154.     c = LF;
  155.     putc(c,outfile);
  156.     }
  157.     fclose(infile);
  158.     fclose(outfile);
  159.     exit(0);
  160.     }
  161.